﻿// this is going to contain the levels which will hook to the logic
// i would like to see a better abstraction here between levels and logic
// but my experience so far is that they are pretty tightly integrated
function Level() {

    // start with no level
    this.custom = new Custom();
    var levelState = { reset: -1, initialzing: 0, running: 1, ending: 3 };
    // initalize to level -1 so it can start at 0 durring advanceLevel
    this.currentLevel = -1;
    this.currentLevelState = -2;

    this.advanceLevel = function () {

        this.currentLevelState++;
        if (this.currentLevelState > levelState.ending)
            this.currentLevelState = levelState.reset;
        // call version level states
        switch (this.currentLevelState) {
            case levelState.reset:
                // TODO: encapsulate currentLevel
                // reset the board once
                this.custom.resetLevel();
                // then advance to  intializing
                this.currentLevel++;
                this.advanceLevel();
                break;
            case levelState.initialzing:
                // this will be called by draw 
                this.custom.initLevel();
                // then auto advance to running
                this.advanceLevel();
                break;
            case levelState.running:
                this.custom.draw();
                break;
            case levelState.ending:
                // determin in endLevel whether or not to advance to another level
                this.custom.endLevel(this);
                break;
        }
    };


    this.initLevel = function (levelToInit) {
        if (levelToInit != null) {
            this.currentLevelState = levelState.reset;
            this.currentLevel = levelToInit

        }
        // this isn't right
        level.advanceLevel();

        // don't feel comfortable enough yet to
        // do 'inheritance' in jscript so I am just calling 
        // stateless functions for levels; we'll see how it goes



    };

    this.startLevel = function (level) {

        if (level === null)
            level = currentLevel;
    };

    this.endLevel = function (level) {
        if (level == null)
            level = currentLevel;
    };
    
    return this;

}